package com.jshcbd.modules.asset.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jshcbd.modules.asset.dto.ReceiveDto;
import com.jshcbd.modules.asset.dto.RenewDto;
import com.jshcbd.modules.asset.dto.RetrocedeDto;
import com.jshcbd.modules.asset.entity.*;
import com.jshcbd.modules.asset.service.IAssetReturnService;
import com.jshcbd.modules.asset.vo.RetrocedeVo;
import com.jshcbd.modules.asset.mapper.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * @author zhoucd
 * @date
 * @description
 */
@Slf4j
@Service
@Transactional
public class AssetReturnServiceImpl extends ServiceImpl<RetrocedeMapper, RetrocedeEntity> implements IAssetReturnService {

    @Autowired
    private RetrocedeMapper retrocedeMapper;

    @Autowired
    private ReceiveMapper receiveMapper;

    @Autowired
    private AssetMapper assetMapper;

    @Autowired
    private RetrocedeItemMapper retrocedeItemMapper;

    @Autowired
    private ReceiveItemMapper receiveItemMapper;




    @Override
    public IPage<RetrocedeVo> pageViewByAssetId(Page<RetrocedeVo> page, String assetId) {
        return retrocedeMapper.queryViewByAssetId(page,assetId);
    }

    @Override
    public IPage<RetrocedeVo> pageView(Page<RetrocedeVo> page, QueryWrapper queryWrapper) {
        return retrocedeMapper.queryPageView(page,queryWrapper);
    }

    @Override
    public IPage<RetrocedeItemEntity> pageRetrocedeItemsByMainId(Page<RetrocedeItemEntity> page, String mainId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("main_id",mainId);
        return retrocedeItemMapper.selectPage(page,queryWrapper);
    }


    @Override
    public void doRetrocede(RetrocedeDto retrocedeDto) {
        RetrocedeEntity entity = new RetrocedeEntity(retrocedeDto);
        retrocedeMapper.insert(entity);
        Arrays.stream(retrocedeDto.getAssetIds()).forEach(id ->{
            RetrocedeItemEntity itemEntity = new RetrocedeItemEntity()
                    .setAssetId(id)
                    .setMainId(entity.getId());
            retrocedeItemMapper.insert(itemEntity);
            if(entity.getIsDamage()){
                assetMapper.updateAsset2Damage(id);
            }else{
                //更新资产状态空闲
                assetMapper.updateAsset2Return(id);
            }
        });

    }

    @Override
    public void doMove(ReceiveDto receive) {
        ReceiveEntity entity = new ReceiveEntity(receive);
        entity.setType(2);
        receiveMapper.insert(entity);
        this.generate(entity,receive.getAssetIds());
    }

    @Override
    public void doRenew(RenewDto renewDto) {
        //获取资产信息
        List<AssetEntity> assetEntityList =  assetMapper.selectList( new QueryWrapper<AssetEntity>().in("id",renewDto.getAssetIds()));
        //获取原领用记录
        String[] rceiveIds = assetEntityList
                .stream()
                .map(entity -> entity.getReceiveId())
                .distinct()
                .toArray(String[]::new);
        List<ReceiveEntity>  receiveEntityList = receiveMapper.selectList(new QueryWrapper<ReceiveEntity>().in("id",rceiveIds));
        receiveEntityList.forEach(receiveEntity -> {
            String sourceId =  receiveEntity.getId();
            receiveEntity.setId(null);
            receiveEntity.setDueDate(renewDto.getDueDate());
            receiveEntity.setReceiver(renewDto.getOperator());
            receiveEntity.setTelphone(renewDto.getPhone());
            receiveEntity.setType(3);
            receiveMapper.insert(receiveEntity);
            String[] assetIds = assetEntityList
                    .stream()
                    .filter(assetEntity -> assetEntity.getReceiveId()
                            .equals(sourceId))
                    .map(assetEntity -> assetEntity.getId())
                    .toArray(String[]::new);
            this.generate(receiveEntity,assetIds);
        });
    }


    private void generate(ReceiveEntity entity,String[] assetIds){
        Arrays.stream(assetIds).forEach(id ->{
            ReceiveItemEntity itemEntity = new ReceiveItemEntity().setAssetId(id).setMainId(entity.getId());
            receiveItemMapper.insert(itemEntity);
            //更新资产状态 领用
            assetMapper.updateAsset2Receive(id,entity.getId(),entity.getLocationBuilding(),entity.getLocationFloor());
        });
    }


}
